package cn.chenfeifx.web.controls;

import cn.chenfeifx.web.enums.AceMode;
import cn.chenfeifx.web.enums.AceTheme;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

class AceHelper {
    private static final String ace_js = "/cn/chenfeifx/web/web/ace/js/ace.min.js";
    private static final String theme_chrome_js = "/cn/chenfeifx/web/web/ace/js/" + AceTheme.CHROME.getName();
    private static final String theme_tomorrow_night_js = "/cn/chenfeifx/web/web/ace/js/" + AceTheme.TOMORROW_NIGHT.getName();

    public static String getHtml(AceMode aceMode) {
        String model_js = "/cn/chenfeifx/web/web/ace/js/" + aceMode.getName();
        try (
                InputStream aceJsStream = AceHelper.class.getResourceAsStream(ace_js);
                InputStream themeChromeJsStream = AceHelper.class.getResourceAsStream(theme_chrome_js);
                InputStream themeTomorrowNightJsStream = AceHelper.class.getResourceAsStream(theme_tomorrow_night_js);
                InputStream modelJsStream = AceHelper.class.getResourceAsStream(model_js);
        ) {
            return "<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n" +
                    "    <title>AceDemo</title>\n" +
                    "    <style>\n" +
                    "        *{\n" +
                    "            padding:0px;\n" +
                    "            margin:0px;\n" +
                    "        }\n" +
                    "        html,body{\n" +
                    "            height: 100%;\n" +
                    "            width: 100%;\n" +
                    "            overflow: hidden;\n" +
                    "        }\n" +
                    "        #editor-container{\n" +
                    "            height: 100%;\n" +
                    "            width: 100%;\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<div id=\"editor-container\"></div>\n" +
                    "<script>" + new String(Objects.requireNonNull(aceJsStream).readAllBytes(), StandardCharsets.UTF_8) + "</script>\n" +
                    "<!--要更改主题，只需包含主题的JavaScript文件-->\n" +
                    "\n" +
                    "<script>" + new String(Objects.requireNonNull(themeChromeJsStream).readAllBytes(), StandardCharsets.UTF_8) + "</script>\n" +
                    "<script>" + new String(Objects.requireNonNull(themeTomorrowNightJsStream).readAllBytes(), StandardCharsets.UTF_8) + "</script>\n" +
                    "<!--默认情况下，编辑器只支持纯文本模式;许多其他语言作为单独的模块可用。在包含该模式的JavaScript文件后:-->\n" +
                    "<script>" + new String(Objects.requireNonNull(modelJsStream).readAllBytes(), StandardCharsets.UTF_8) + "</script>\n" +
                    "<script>\n" +
                    "    // https://cdnjs.com/libraries/ace\n" +
                    "\n" +
                    "    var editor = ace.edit(\"editor-container\");\n" +
                    "    // 使用主题:\n" +
                    "    editor.setTheme(\"ace/theme/chrome\");\n" +
                    "    // 使用模式\n" +
                    "    editor.session.setMode(\"ace/mode/java\");\n" +
                    "\n" +
                    "    //字体大小\n" +
                    "    editor.setFontSize(14);\n" +
                    "    editor.setOptions({\n" +
                    "        showGutter: true, // 显示左侧内容（行号和折叠内容）\n" +
                    "        showFoldWidgets: true, // 显示折叠小部件\n" +
                    "        showLineNumbers: true, // 是否显示行号\n" +
                    "        readOnly: false, // 设置只读（true时只读，用于展示代码）\n" +
                    "        wrap: 'off', // 自动换行,off：关闭 , free：开启\n" +
                    "        enableBasicAutocompletion: true,\n" +
                    "        enableSnippets: true,\n" +
                    "        enableLiveAutocompletion: true,\n" +
                    "        showPrintMargin: false, // 打印边距\n" +
                    "    });" +
                    "\n" +
                    "    // 设置模式\n" +
                    "    function setEditorMode(mode){\n" +
                    "        editor.session.setMode(mode);\n" +
                    "    }\n" +
                    "\n" +
                    "    // 设置内容\n" +
                    "    function setEditorValue(value){\n" +
                    "        editor.setValue(value,1);\n" +
                    "        // editor.setValue(\"text2\", -1); // set value and move cursor to the start of the text\n" +
                    "        // editor.session.setValue(\"the new text here\"); // set value and reset undo history\n" +
                    "    }\n" +
                    "\n" +
                    "    // 获取内容\n" +
                    "    function getEditorValue(value){\n" +
                    "        return editor.getValue(); // or session.getValue\n" +
                    "    }\n" +
                    "\n" +
                    "    // 删除选中的内容\n" +
                    "    function removeSelectRange(){\n" +
                    "        var range = editor.getSelectionRange();\n" +
                    "        editor.remove(range);\n" +
                    "    }\n" +
                    "\n" +
                    "    // 监听窗口大小变化\n" +
                    "    window.addEventListener('resize', function() {\n" +
                    "        // 在这里执行窗口大小变化后的操作\n" +
                    "        editor.resize();\n" +
                    "    });\n" +
                    "\n" +
                    "    // 监听内容变化\n" +
                    "    editor.on('change', function(delta) {\n" +
                    "        // delta.start, delta.end, delta.lines, delta.action\n" +
                    "        try{\n" +
                    "            window.AceView.textChange(editor.getValue()); // 通知javafx(AceView)程序内容发生变化。\n" +
                    "        }catch(err){\n" +
                    "        }\n" +
                    "    });\n" +
                    "\n" +
                    "</script>\n" +
                    "</body>\n" +
                    "</html>";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}